package com.teamsapp.article.controller;

import com.teamsapp.article.dto.*;
import com.teamsapp.article.dto.ApiResponse;
import com.teamsapp.article.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
@RequiredArgsConstructor
@Tag(name = "文章管理", description = "文章的CRUD操作")
public class ArticleController {
    private final ArticleService articleService;

    @GetMapping("/getAllArticles")
    @Operation(summary = "获取文章列表（分页）")
    public ResponseEntity<ApiResponse<PageResponse<ArticleDTO>>> getAllArticles(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int page_size) {
        PageResponse<ArticleDTO> result = articleService.getAllArticles(page, page_size);
        return ResponseEntity.ok(ApiResponse.success("获取文章列表成功", result));
    }

    @GetMapping("/getArticleById/{id}")
    @Operation(summary = "根据ID获取文章")
    public ResponseEntity<ApiResponse<ArticleDTO>> getArticleById(@PathVariable String id) {
        ArticleDTO article = articleService.getArticleById(id);
        return ResponseEntity.ok(ApiResponse.success("获取文章详情成功", article));
    }

    @PostMapping("/createArticle")
    @Operation(summary = "创建新文章")
    public ResponseEntity<ApiResponse<ArticleDTO>> createArticle(@Valid @RequestBody CreateArticleDTO createArticleDTO) {
        ArticleDTO article = articleService.createArticle(createArticleDTO);
        return ResponseEntity.status(HttpStatus.CREATED)
            .body(ApiResponse.success("创建文章成功", article));
    }

    @PutMapping("/updateArticle/{id}")
    @Operation(summary = "更新文章")
    public ResponseEntity<ApiResponse<ArticleDTO>> updateArticle(
            @PathVariable String id,
            @Valid @RequestBody UpdateArticleDTO updateArticleDTO) {
        ArticleDTO article = articleService.updateArticle(id, updateArticleDTO);
        return ResponseEntity.ok(ApiResponse.success("更新文章成功", article));
    }

    @DeleteMapping("/deleteArticle/{id}")
    @Operation(summary = "删除文章")
    public ResponseEntity<ApiResponse<Void>> deleteArticle(@PathVariable String id) {
        articleService.deleteArticle(id);
        return ResponseEntity.ok(ApiResponse.success("删除文章成功", null));
    }

    @GetMapping("/getPublishedArticles")
    @Operation(summary = "获取已发布的文章列表（分页）")
    public ResponseEntity<ApiResponse<PageResponse<ArticleDTO>>> getPublishedArticles(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int page_size) {
        PageResponse<ArticleDTO> result = articleService.getPublishedArticles(page, page_size);
        return ResponseEntity.ok(ApiResponse.success("获取已发布文章列表成功", result));
    }
}